﻿using UnityEngine;
using System.Collections;
using System.Security.Cryptography;
using System;
/// <summary>
/// 游戏状态机
/// </summary>
public enum GameState
{
    start,
    ready,
    gameing,
    dieing,
    gameover
}
public class GameCtrl : MonoBehaviour
{
    public GameObject getReady, gameStart, gameOver, game, btn;
    public static GameCtrl Inst;
    public UILabel TopScore, endScore;
    TweenPosition gameOverlogoTween;
    // 状态机实例
    public static GameState gameState = GameState.start;
    void Start()
    {
        gameOverlogoTween = gameOver.GetComponent<TweenPosition>();
    }

    void Awake()
    {
        Inst = this;
    }
    // Update is called once per frame
    void Update()
    {
        gameStart.SetActive(false);
        getReady.SetActive(false);
        game.SetActive(false);
        btn.SetActive(false);

        switch(gameState)
        {
            case GameState.start:
                {
                    gameStart.SetActive(true);
                    btn.SetActive(true);
                }
                break;

            case GameState.ready:
                {
                    score = 0;
                    TopScore.text = score.ToString();
                    getReady.SetActive(true);
                    game.SetActive(true);

                    if(Input.GetMouseButtonUp(0))
                    {
                        getReady.SetActive(false);
                        gameState = GameState.gameing;
                        Hero.Inst.up();
                    }
                }
                break;

            case GameState.gameing:
                {
                    game.SetActive(true);
                }
                break;

            case GameState.gameover:
                {
                    endScore.text = TopScore.text;
                    btn.SetActive(true);
                }
                break;
        }
    }
    int score = 0;
    int bestScore = 0;
    public AudioClip SFX_point;
    public void addScore()
    {
        score++;
        TopScore.text = score.ToString();
        playSFX(SFX_point);
    }
    /// <summary>
    /// 重新开始游戏委托
    /// </summary>
    public delegate void RestDelegate();
    /// <summary>
    /// 重新开始游戏委托实例.游戏开始.游戏重新开始
    /// </summary>
    public static RestDelegate restHandler;
    bool isready = false;
    public void gameStartBtn()
    {
        if(gameState == GameState.start)
        {
            //向网页请求开始
            Application.ExternalCall("gameStart");
#if UNITY_EDITOR
            gameStartCallback(0);
#endif
        }
        else
        {
            gameStartCallback(bestScore);
        }
    }
    /// <summary>
    /// 游戏开始网页回调口
    /// 整个游戏最开始调用一次
    /// </summary>
    /// <param name="jbestScore">当前用户最好分</param>
    void gameStartCallback(int jbestScore)
    {
        bestScoreLabel.text = jbestScore.ToString();
        bestScore = jbestScore;
        Invoke("ready", 0.1f);
        restHandler();
        gameOverlogoTween.PlayReverse();
        gameOver.audio.Play();
    }
    void ready()
    {
        gameState = GameState.ready;
    }
    public void playSFX(AudioClip a)
    {
        audio.PlayOneShot(a);
    }
    public void playSFX(AudioClip a, float d)
    {
        audio.clip = a;
        audio.PlayDelayed(d);
    }
   
    /// <summary>
    /// 游戏内部结束.
    /// </summary>
    public void gameoverOnece()
    {
        //合成key
        GameCtrl.gameState = GameState.gameover;
        string key = score.ToString() + "_SCNTV_GAME_";
        key = GetMD5Hash(key);
        //调用网页,上传成绩
        Application.ExternalCall("gameOver", score, key);
        //出现结算板
        gameOver.audio.Play();//结算板出现声音
        gameOverlogoTween.PlayForward();//从上到下
        newob.SetActive(false);//把new不可见

        //计算是不是最好分
        if(score > bestScore)
        {
            bestScore = score;
            newob.SetActive(true);
        }

        bestScoreLabel.text = ((int)bestScore).ToString();
    }
    string GetMD5Hash(string strPwd)
    {
        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] data = System.Text.Encoding.Default.GetBytes(strPwd);
        byte[] md5data = md5.ComputeHash(data);
        string ret = BitConverter.ToString(md5data).Replace("-", "");
        return ret.ToLower();
    }
    public UILabel bestScoreLabel;
    public GameObject newob;

}
